package com.cdtu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cdtu.entity.PersonalPlan;
import com.cdtu.entity.SysEmpRole;
import com.cdtu.entity.SysEmployee;
import com.cdtu.service.PersonalPlanService;
import com.cdtu.service.SysEmployeeService;
import com.cdtu.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/pplan")
//解决跨域问题
@CrossOrigin
public class PersonalPlanController extends BaseController{
    @Autowired
    private PersonalPlanService personalPlanService;
    @Autowired
    private SysEmployeeService sysEmployeeService;

    @RequestMapping("/list")
    public ModelAndView empList(ModelAndView modelAndView) {

        List<PersonalPlan> personalList = personalPlanService.list();
        List<SysEmployee> sysEmployeeList = sysEmployeeService.list();
        //返回数据
        modelAndView.addObject("personalList", personalList);
        modelAndView.addObject("sysEmployeeList", sysEmployeeList);
        //2.跳转页面;
        modelAndView.setViewName("train/personPlan/personPlanList");
        return modelAndView;
    }


    @RequestMapping("/toAddUI")
    public ModelAndView toAddSysEmp(ModelAndView modelAndView) {
        //2.跳转页面;
        modelAndView.setViewName("train/personPlan/addPersonPlan");
        return modelAndView;
    }

    @Transactional
    @RequestMapping("/add")
    public ModelAndView addEmpUI(String state,PersonalPlan personalPlan,ModelAndView modelAndView) {

        System.out.println(personalPlan);
        System.out.println(state);
        personalPlan.setStatus(Integer.valueOf(state));
        personalPlan.setCreactTime(new Date());
        personalPlanService.save(personalPlan);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/pplan/list");
        return modelAndView;
    }

    @RequestMapping("/toUpdateUI/{planId}")
    public ModelAndView toUpdateUi(ModelAndView modelAndView,@PathVariable("planId") Long planId) {
        List<SysEmployee> sysEmployeeList = sysEmployeeService.list();
        PersonalPlan personalPlan = personalPlanService.getById(planId);
        //返回数据
        modelAndView.addObject("personalPlan", personalPlan);
        modelAndView.addObject("sysEmployeeList", sysEmployeeList);
        //跳转页面;
        modelAndView.setViewName("train/personPlan/updatePersonPlan");
        return modelAndView;
    }

    @Transactional
    @RequestMapping("/update")
    public ModelAndView update(ModelAndView modelAndView,String state,PersonalPlan personalPlan) {
        personalPlan.setStatus(Integer.valueOf(state));
        System.out.println(personalPlan);
        personalPlanService.updateById(personalPlan);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/pplan/list");
        return modelAndView;
    }

    @Transactional
    @RequestMapping("/delete/{planId}")
    public ModelAndView delete(@PathVariable("planId") Long planId, ModelAndView modelAndView) {

        boolean remove = personalPlanService.removeById(planId);
        modelAndView.setViewName("redirect:/pplan/list");
        return modelAndView;
    }
}
